<?php
namespace app\api\controller\juyin;

use think\Controller;

class Juyinpay extends Controller
{
    /**
 * [聚银]支付接口调测例子
 * ================================================================
 * index 进入口，方法中转
 * unionPay 银联快捷支付
 * ================================================================
 */
    private $orderUrl         = 'http://39.107.87.17/pay/juyin/unionpay/order'; // 下单地址
    private $orderQueryUrl    = 'http://39.107.87.17/pay/juyin/orderquery/orderinfo'; // 查询地址
    private $daifuUrl         = 'http://39.107.87.17/pay/juyin/daifu/index'; // 代付申请地址
    private $daifuYuEUrl      = 'http://39.107.87.17/pay/juyin/daifuyue/index'; // 代付查询余额地址
    private $daifuStaUrl      = 'http://39.107.87.17/pay/juyin/daifusta/index'; // 代付查询订单地址
    private $callbackUrl      = 'http://39.107.87.17/pay/juyin/callback/index'; // 回调地址
    
    private $merchantId       = '10001'; // 商户号
    private $md5key           = '8d0298d01a6629e0b8b37b89c0f00324'; // 交易签名密码
    private $daifumd5key      = '814234044bd17ea6ff0eb3bb155e3a91'; // 代付签名秘钥
    private $daifuPwd         = '25811629db5afef3e5d012312e21b521'; // 代付密码
    //
    // 进入口，方法中转
    public function index() {
        return $this->fetch();
    }
    
    /*
     * 银联快捷
     */
    
    // 返回银联快捷订单创建页
    public function unionpay() {
        date_default_timezone_set('Asia/Shanghai');
        $orderId = date('YmdHis', time()) . rand(10, 99);
        $this->assign('orderId', $orderId);
        return $this->fetch();
    }
    
    // 银联快捷支付请求
    public function RequestUnionpay() {
        $data = input('post.'); 
        $data['merchant_id'] = $this->merchantId;  
        $singVal = $this->createSign($data, false);
        $data['signature'] = $singVal;
        $result = $this->curl($this->orderUrl, $data);
        dump($data);
        dump($result);
    }
    
    /*
     * 订单查询
     */
    
    // 返回查询订单页
    public function orderQuery() {
        return $this->fetch();
    }
    
    // 查询订单请求
    public function RequestOrderQuery() {
        $data = input('post.'); 
        $singVal = $this->createSign($data, false);
        $data['signature'] = $singVal;
        $result = $this->curl($this->orderQueryUrl, $data);
        dump($data);
        dump($result);  
    }
    
    /*
     * 代付
     */
     
    // 返回代付页
    public function daifu() {
        date_default_timezone_set('Asia/Shanghai');
        $memOrder = date('YmdHis', time()) . rand(10, 99);
        $this->assign('memOrder', 'DF' . $memOrder);
        return $this->fetch();
    }
    
    // 代付请求
    public function RequestDaifu() {
        $data = input('post.'); 
        $data['password'] = $this->daifuPwd;
        $singVal = $this->createSign($data, true);
        $data['signature'] = $singVal;
        $result = $this->curl($this->daifuUrl, $data);
        dump('POST传参：');
        dump($data);
        dump('返回结果：');
        dump($result);  
    }
    
    // 返回代付查询余额页
    public function daifuYuE() {
        return $this->fetch();
    }
    
    // 代付请求查询余额
    public function RequestDaifuYuE() {
        $data = input('post.'); 
        $singVal = $this->createSign($data, true);
        $data['signature'] = $singVal;
        $result = $this->curl($this->daifuYuEUrl, $data);
        dump('POST传参：');
        dump($data);
        dump('返回结果：');
        dump($result);  
    }
    
    // 返回代付查询订单页
    public function daifuSta() {
        return $this->fetch();
    }
    
    // 代付请求查询订单
    public function RequestDaifuSta() {
        $data = input('post.'); 
        $singVal = $this->createSign($data, true);
        $data['signature'] = $singVal;
        $result = $this->curl($this->daifuStaUrl, $data);
        dump('POST传参：');
        dump($data);
        dump('返回结果：');
        dump($result);  
    }
    
    // 模拟回调
    public function callback() {
        return $this->fetch();
    }
    
    // 模拟回调 接收数据
    public function reqCallback() {
        $data = input('post.');
        $result = $this->curl($this->callbackUrl, $data);
    }
    
    // 创建签名
    protected function createSign($list, $isDaifu) {
        ksort($list);
        $md5str = '';
        foreach ($list as $key => $val) {
            if(!empty($val)){
                $md5str = $md5str . $key . '=' . $val . '&';
            }
        }
        if($isDaifu) {
            $md5key = $this->daifumd5key;
        } else {
            $md5key = $this->md5key;
        }
        $sign = strtoupper(md5($md5str . 'key=' . $md5key));
        return $sign;
    }
    
    // 使用post的传输
    public function curl($url, $data) {
        // 启动一个CURL会话
        $ch = curl_init();
        // 设置curl允许执行的最长秒数
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        // 忽略证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        // 获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        // 发送一个常规的POST请求。
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, 0); // 是否需要头部信息（否）
        // 执行操作
        $result = curl_exec($ch);

        if($result) {
            curl_close($ch);
            // 将返回json转换为数组
            $arrResult = json_decode($result, true);
//            if(!is_array($arrResult)) {
//                $arrResult['errcode']=1;
//                $arrResult['msg']='服务器繁忙，请稍候重试';
//            }
        } else {
            $arrResult = curl_error($ch);
            curl_close($ch);
//            $arrResult['errcode']=1;
//            $arrResult['msg']='服务器繁忙，请稍候重试';
        }
        // 返回数据
        return $arrResult;
    }
    
    // 成功支付通知处理
    public function callbak() {
        
    }
    
    // 前台跳转地址,用户支付成功后跳转的地址
    public function turny() {
        
    }
}

